package com.cqs.example.io.example2;

import com.alibaba.fastjson.util.IOUtils;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

@Slf4j
public class TimeServerHandler2 implements Runnable {

    private Socket socket;

    public TimeServerHandler2(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        InputStream is = null;
        OutputStream os = null;
        try {
            is = socket.getInputStream();
            int available = is.available();
            byte[] inBytes = new byte[available];
            is.read(inBytes);
            String msg = available +  "server 接受消息:" + new String(inBytes) + "--- " + is.available();
            log.info(msg);
            os = socket.getOutputStream();
            os.write(msg.getBytes());
        } catch (Exception e) {
            log.warn("socket通信异常", e);
        } finally {
//            IOUtils.close(socket);
            log.info(socket +" close");
        }
    }
}